const db = wx.cloud.database()
const App = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    myInfo: {
      avatarUrl: "",
      nickName: "",
      identity: "",
      college: "",
      gender: "",
    },
    list: [{
        pagePath: '/pages/students/home/index',
        iconPath: "../../images/icon/home.png",
        color: "text-gray",
        text: '首页',
      },
      {
        pagePath: '/pages/students/practice/index/index',
        iconPath: "../../images/icon/practice.png",
        color: "text-gray",
        text: '实践',
      },
      {
        pagePath: '/pages/students/teaching/index',
        iconPath: "../../images/icon/teaching.png",
        color: "text-gray",
        text: '教学',
      },
      {
        pagePath: '/pages/students/my/index',
        iconPath: "../../images/icon/my-action.png",
        color: "text-blue",
        text: '我的',

      }
    ],
    userContentList: [{
      id: 0,
      title: "",
      text: "",
      iconbg: "#D6F4FA",
      iconcolor: "#31C9E8",
      icon: "icon-shenfenleibie",
      icon2: "",
    },
    {
      id: 1,
      title: "学院",
      text: "",
      iconbg: "#FFEDD4 ",
      iconcolor: "#FFA726",
      icon: "icon-mingxiaomingshi",
      icon2: "",
    }, {
      id: 2,
      title: "班级",
      text: "",
      iconbg: "#FAD5E8",
      iconcolor: "#E72F8C",
      icon: "icon-xueyuan",
      icon2: "",
    }, {
      id: 3,
      title: "实训",
      text: "基地",
      iconbg: "#D6FADB",
      iconcolor: "#31E749",
      icon: "icon-shenfenleibie",
      icon2: "",
    },
  ],
    // 身份信息
    identityArray: [{
      name: '学生',
      id: 'Students'
    }, {
      name: '教师',
      id: 'Teachers'
    }, {
      name: '管理员',
      id: 'Administrator'
    }],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('我的页面');
    let get_openid = App.cache.getOpenID()
    console.log('用户openid', get_openid);
    // 获取用户信息
    let UserInfo = App.cache.getUserInfo()
    console.log('获取用户信息:', UserInfo);
    delete UserInfo._id
    this.setData({
      get_openid: get_openid,
      UserInfo: UserInfo
    })
    this.getMyclass()
    this.getPracticeBase()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log('我的页面');
    // 路由回来刷新页面
    this.myInfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  myInfo() {
    let that = this
    let identityArray = that.data.identityArray
    // 获取用户信息
    let UserInfo = App.cache.getUserInfo()
    console.log('获取用户信息:', UserInfo);
    // 根据身份id获取对应的身份
    let identityobj = identityArray.find(function (identityobj) {
      return identityobj.id === UserInfo.identityId
    })
    console.log("身份 ", identityobj.name);
    this.setData({
      ['myInfo.avatarUrl']: UserInfo.avatarUrl,
      ['myInfo.nickName']: UserInfo.nickName,
      ['myInfo.college']: UserInfo.collegeId,
      ['myInfo.identity']: identityobj.name,
      'userContentList[0].title': identityobj.name
    })
  },

  // 个人信息弹窗 start
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  // 个人信息弹窗 end


  // 获取专业班级
  // 我的加入
  getMyclass() {
    let that = this
    db.collection('myclass').where({
        'list.account': that.data.UserInfo.account,
        classType: '专业班级'
      })
      .get({
        success: function (res) {
          let list = res.data
          console.log('我加入的班级', list[0])
          that.setData({
            myclass: list[0]
          })
        }
      })
  },
  // 获取实训基地
  // 获取实训基地
  getPracticeBase() {
    let that = this
    db.collection('practiceBase').where({
        'members.account': that.data.UserInfo.account
      })
      .get({
        success: function (res) {
          console.log('获取实践基地', res.data[0])
          that.setData({
            baseList: res.data[0]
          })
        }
      })
  },
  // 复制gitee地址
  CopyLink(e) {
    wx.setClipboardData({
      data: e.currentTarget.dataset.link,
      success: res => {
        wx.showToast({
          title: '地址已复制',
          icon: "none",
          duration: 1000,
        })
      }
    })
  },
  // 路由跳转修改个人信息
  updateMy() {
    App.route.navTo('/pages/updateMy/index')
  },
  // 我的班级
  myclass() {
    App.route.navTo('/pages/myclass/list/index')

  },
  // 关于程序
  about() {
    App.route.navTo('/pages/about/index')
  },
  // 日志
  log(){
    App.route.navTo('/pages/log/index')
  }
})